package org.example.leetcpde;

import org.checkerframework.checker.units.qual.K;
import org.junit.jupiter.api.Test;

import javax.crypto.Mac;
import java.util.stream.IntStream;

public class maxArea
        implements IArray {

    @Test
    void maxArea() {

        int[] height = parse("[3690,7683,9156,8637,6242,1881,9505,3803,3840,7101,3490,217,9335,5318]");
        int i = maxArea(height);
        System.out.println(i);
    }

    public int maxArea(int[] height) {
        int max = 0;
        for (int i = 0, j = height.length - 1; i < j; ) {
            int h1 = height[i];
            int h2 = height[j];
            int v = Math.min(h1, h2) * (j - i);
            max = Math.max(v, max);
            if (h1 <= h2) {
                i++;
            } else {
                j--;
            }
        }

        return max;
    }

    public int maxAreaV2(int[] height) {
        int len = height.length;

        int[][] dp = new int[len][len + 1];
        for (int i = 1; i < len; i++) {
            for (int j = i + 1; j <= len; j++) {
                int h1 = height[i - 1];
                int h2 = height[j - 1];
                int v = Math.min(h1, h2) * (j - i);
                dp[i][j] = IntStream.of(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1], v).max().getAsInt();
            }
        }

        return dp[len - 1][len];
    }
}
